package itbour.onetouchshow.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import itbour.onetouchshow.R;
import itbour.onetouchshow.utils.UIUtils;


@SuppressLint("AppCompatCustomView")
public class RoundImageView extends LinearLayout {

    public static final int TYPE_CIRCLE = 0;
    public static final int TYPE_ROUND = 1;
    private ImageView imageView;
    /**
     * 圆角的大小
     */
    private int mBorderRadius;
    /**
     * 图片的类型，圆形or圆角
     */
    private int type;

    public RoundImageView(Context context) {
        this(context, null);
    }


    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
        mBorderRadius = a.getDimensionPixelSize(
                R.styleable.RoundImageView_borderRadius, (int) TypedValue
                        .applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                UIUtils.dip2px(10), getResources()
                                        .getDisplayMetrics()));// 默认为10dp
        type = a.getInt(R.styleable.RoundImageView_type, TYPE_ROUND);// 默认为圆角
        a.recycle();
        requestLayout();
        View inflate = inflate(getContext(), R.layout.base_round_image, this);
        RCRelativeLayout rcRelativeLayout = inflate.findViewById(R.id.rc_ry);
        rcRelativeLayout.setmBorderRadius(mBorderRadius);
        rcRelativeLayout.setmRoundAsCircle(type == TYPE_CIRCLE);
        imageView = inflate.findViewById(R.id.id_ig_show);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

    }

    public void setType(int type) {
        if (this.type != type) {
            this.type = type;
            invalidate();
        }
    }

    public void setBorderRadius(int borderRadius) {
        if (this.mBorderRadius != borderRadius) {
            this.mBorderRadius = borderRadius;
            invalidate();
        }
    }


    public ImageView getImageView() {
        return imageView;
    }
}